package cn.tedu.hospitalqd.controller;

import cn.tedu.hospitalqd.pojo.dto.PharmacyAddNewDTO;
import cn.tedu.hospitalqd.pojo.dto.PharmacyUpdateDTO;
import cn.tedu.hospitalqd.pojo.vo.PharmacyListItemVO;
import cn.tedu.hospitalqd.pojo.vo.PharmacyStandardVO;
import cn.tedu.hospitalqd.service.PharmacyService;
import cn.tedu.hospitalqd.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/pharmacy")
@Api(tags = "09.取药表管理模块")
@Validated
public class PharmacyController {
    @Autowired
    private PharmacyService pharmacyService;

    @PostMapping("/add-new")
    @ApiOperation("添加取药表")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid PharmacyAddNewDTO pharmacyAddNewDTO){
        log.debug("开始处理【添加取药表】的请求，参数：{}", pharmacyAddNewDTO);
        pharmacyService.addNew(pharmacyAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除取药表")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "取药表ID",required = true,dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "删除取药表失败，ID值无效！")
                             @PathVariable Long id){
        log.debug("开始处理【根据ID删除取药表】的请求，参数：{}", id);
        pharmacyService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改取药表详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "取药表ID",required = true,dataType = "lonmg")
    })
    public JsonResult updateInfoById(@Range(min = 1,message = "修改取药表详情失败，ID值无效！")
                                     @PathVariable Long id, @Valid PharmacyUpdateDTO pharmacyUpdateDTO){
        log.debug("开始处理【修改取药表详情】的请求，参数：{}", pharmacyUpdateDTO);
        pharmacyService.updateInfoById(id, pharmacyUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询取药表详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "取药表ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "获取取药表详情失败，ID值无效！")
                                      @PathVariable Long id) {
        log.debug("开始处理【根据ID查询取药表详情】的请求，参数：{}", id);
        PharmacyStandardVO queryResult = pharmacyService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9080/albums
    @GetMapping("")
    @ApiOperation("查询取药表列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list() {
        log.debug("根据ID查询取药表详情");
        List<PharmacyListItemVO> list = pharmacyService.list();
        return JsonResult.ok(list);
    }




}
